<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
class Common {
    
    //构造函数
    public function  __construct() {
    	$this->admin_config();
    	$this->check_admin();
    	$this->show_menu();
    	$this->check_priv();
    	$this->c_top();
    	$this->check_token();
    }
    
    // 获取管理员信息
    private function admin_config() {
        $admin = D('admin')->where('roleid', session('roleid'))->find();
    	View::assign(['site'=>get_config(),'admin'=>$admin]);
    }
    
    
    private function c_top() {
        $request= request();
        if($request->action() == 'top'){
            Jump::error('该方法名菜单属性');
        }
        
    }
    
    // 检查是否登录
    private function check_admin() {
    	if(!session('adminid')) {
    		Jump::error('登录过期', url('/admin/login'));
    	}
    }
    
    
    // 表单令牌
    private function check_token() {
    	 if(request()->action()=='del'||'upload'||'upload_wang'||'uninstall') return false;
         if (request()->isPost()){
    	     $check = request()->checkToken('__token__');
             if(false === $check) {
                Jump::error('token错误，请检查');
             }
         }
    }
    
    

    // 输出菜单
    private function show_menu() {
    	$menu_list = D('app_menu')->where(['parentid'=>'0','status'=>'1'])->order('sort acs')->select()->all();
    	foreach ($menu_list as $key => $value) {
    		$child = D('app_menu')->where(['parentid'=>$value['id'],'status'=>'1'])->order('sort acs')->select()->all();
    		foreach ($child as $k=>$v) {
    			if(session('roleid') != 1) {
    				$data = D('admin_role_priv')->field('roleid')->where(['roleid'=>session('roleid'), 'm'=>$v['m'], 'c'=>$v['c'], 'a'=>$v['a']])->find();
    				if(!$data) unset($child[$k]);
    			}
    		}
    		if($child) {
    			$menu_list[$key]['child'] = $child;
    		} else {
    			unset($menu_list[$key]);
    		}
    	}
    	View::assign(['menu'=>$menu_list]);
    }
    
    
    
    // 检查权限
    private function check_priv() {
    	$request= request();
    	$module = app('http')->getName();
    	if($request->controller() =='index' && in_array($request->action(), array('login', 'index'))) return false;
    	if(session('roleid') == 1) return false;
    	if(strpos($request->action(), 'index') === 0) return false;
    	
    	if (!request()->isPost()) return true;
    	
    	$r = D('admin_role_priv')->where(['m'=>$module,'c'=>$request->controller(),'a'=>$request->action(),'roleid'=>session('roleid')])->find();
    	if(!$r) {
    		Jump::error('当前环境权限不允许操作！');
    	}
    }
	

}
